<template>
  <div class="register-page">
    <div class="register-container">
      <!-- 注册表单 -->
      <div class="register-form-wrapper">
        <div class="form-header">
          <div class="logo">
            <el-icon :size="40">
              <Shop/>
            </el-icon>
            <h1>Easy Mart</h1>
          </div>
          <p class="welcome-text">创建新账号</p>
        </div>

        <el-form
            ref="registerFormRef"
            :model="registerForm"
            :rules="registerRules"
            class="register-form"
            size="large"
        >
          <el-form-item prop="username">
            <el-input
                v-model="registerForm.username"
                clearable
                placeholder="请输入用户名"
                prefix-icon="User"
            />
          </el-form-item>

          <el-form-item prop="nickname">
            <el-input
                v-model="registerForm.nickname"
                clearable
                placeholder="请输入昵称"
                prefix-icon="Avatar"
            />
          </el-form-item>

          <el-form-item prop="email">
            <el-input
                v-model="registerForm.email"
                clearable
                placeholder="请输入邮箱"
                prefix-icon="Message"
            />
          </el-form-item>

          <el-form-item prop="phone">
            <el-input
                v-model="registerForm.phone"
                clearable
                placeholder="请输入手机号"
                prefix-icon="Phone"
            />
          </el-form-item>

          <el-form-item prop="password">
            <el-input
                v-model="registerForm.password"
                clearable
                placeholder="请输入密码"
                prefix-icon="Lock"
                show-password
                type="password"
            />
          </el-form-item>

          <el-form-item prop="confirmPassword">
            <el-input
                v-model="registerForm.confirmPassword"
                clearable
                placeholder="请确认密码"
                prefix-icon="Lock"
                show-password
                type="password"
                @keyup.enter="handleRegister"
            />
          </el-form-item>

          <el-form-item>
            <el-checkbox v-model="agreeTerms">
              我已阅读并同意
              <el-button type="text">《用户协议》</el-button>
              和
              <el-button type="text">《隐私政策》</el-button>
            </el-checkbox>
          </el-form-item>

          <el-form-item>
            <el-button
                :disabled="!agreeTerms"
                :loading="loading"
                class="register-btn"
                type="primary"
                @click="handleRegister"
            >
              注册
            </el-button>
          </el-form-item>

          <div class="form-footer">
            <span>已有账号？</span>
            <el-button type="text" @click="goToLogin">立即登录</el-button>
          </div>
        </el-form>
      </div>

      <!-- 右侧装饰 -->
      <div class="register-decoration">
        <div class="decoration-content">
          <h2>加入我们</h2>
          <p>开启您的购物之旅</p>
          <p>享受专属会员权益</p>
          <p>发现更多精彩商品</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import {reactive, ref} from 'vue'
import {useRouter} from 'vue-router'
import {ElMessage} from 'element-plus'
import {Shop} from '@element-plus/icons-vue'
import {register} from '@/api/user'

// 路由相关
const router = useRouter()

// 响应式数据
const loading = ref(false)
const agreeTerms = ref(false)
const registerFormRef = ref()

// 注册表单
const registerForm = reactive({
  username: '',
  nickname: '',
  email: '',
  phone: '',
  password: '',
  confirmPassword: ''
})

// 表单验证规则
const registerRules = {
  username: [
    {required: true, message: '请输入用户名', trigger: 'blur'},
    {min: 3, max: 20, message: '用户名长度在3到20个字符', trigger: 'blur'},
    {pattern: /^[a-zA-Z0-9_]+$/, message: '用户名只能包含字母、数字和下划线', trigger: 'blur'}
  ],
  nickname: [
    {required: true, message: '请输入昵称', trigger: 'blur'},
    {min: 2, max: 20, message: '昵称长度在2到20个字符', trigger: 'blur'}
  ],
  email: [
    {required: true, message: '请输入邮箱', trigger: 'blur'},
    {type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur'}
  ],
  phone: [
    {required: true, message: '请输入手机号', trigger: 'blur'},
    {pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号', trigger: 'blur'}
  ],
  password: [
    {required: true, message: '请输入密码', trigger: 'blur'},
    {min: 6, max: 20, message: '密码长度在6到20个字符', trigger: 'blur'}
  ],
  confirmPassword: [
    {required: true, message: '请确认密码', trigger: 'blur'},
    {
      validator: (rule, value, callback) => {
        if (value !== registerForm.password) {
          callback(new Error('两次输入的密码不一致'))
        } else {
          callback()
        }
      },
      trigger: 'blur'
    }
  ]
}

// 处理注册
const handleRegister = async () => {
  try {
    // 表单验证
    await registerFormRef.value.validate()

    if (!agreeTerms.value) {
      ElMessage.warning('请先同意用户协议和隐私政策')
      return
    }

    loading.value = true

    // 调用注册接口
    await register({
      username: registerForm.username,
      nickname: registerForm.nickname,
      email: registerForm.email,
      phone: registerForm.phone,
      password: registerForm.password
    })

    ElMessage.success('注册成功，请登录')

    // 跳转到登录页面
    router.push('/login')
  } catch (error) {
    if (error !== false) {
      console.error('注册失败:', error)
      ElMessage.error(error.response?.data?.message || '注册失败，请重试')
    }
  } finally {
    loading.value = false
  }
}

// 跳转到登录页面
const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-page {
  min-height: 100vh;
  background: var(--gradient-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
}

.register-container {
  display: flex;
  background: var(--bg-card);
  border-radius: var(--radius-2xl);
  box-shadow: var(--shadow-xl);
  overflow: hidden;
  max-width: 1000px;
  width: 100%;
  min-height: 600px;
  border: 1px solid var(--border-light);
}

/* 注册表单区域 */
.register-form-wrapper {
  flex: 1.2;
  padding: 40px 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  overflow-y: auto;
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 15px;
}

.logo .el-icon {
  color: #409eff;
}

.logo h1 {
  margin: 0;
  font-size: 28px;
  color: #303133;
  font-weight: bold;
}

.welcome-text {
  margin: 0;
  font-size: 18px;
  color: #606266;
}

.register-form {
  max-width: 400px;
  margin: 0 auto;
  width: 100%;
}

.register-form .el-form-item {
  margin-bottom: 20px;
}

.register-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  border-radius: 6px;
}

.form-footer {
  text-align: center;
  margin-top: 15px;
  color: #909399;
}

.form-footer .el-button {
  color: #409eff;
  padding: 0;
}

/* 右侧装饰区域 */
.register-decoration {
  flex: 0.8;
  background: linear-gradient(135deg, #36cfc9 0%, #409eff 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: relative;
  overflow: hidden;
}

.register-decoration::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="1.5" fill="rgba(255,255,255,0.1)"/></svg>') repeat;
  animation: float 25s infinite linear;
}

@keyframes float {
  0% {
    transform: translateX(0) translateY(0) rotate(0deg);
  }
  100% {
    transform: translateX(-50px) translateY(-50px) rotate(360deg);
  }
}

.decoration-content {
  text-align: center;
  z-index: 1;
  position: relative;
}

.decoration-content h2 {
  font-size: 32px;
  margin: 0 0 20px 0;
  font-weight: 300;
}

.decoration-content p {
  font-size: 16px;
  margin: 12px 0;
  opacity: 0.9;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
    margin: 10px;
  }

  .register-decoration {
    order: -1;
    min-height: 180px;
  }

  .decoration-content h2 {
    font-size: 24px;
  }

  .decoration-content p {
    font-size: 14px;
    margin: 8px 0;
  }

  .register-form-wrapper {
    padding: 30px 30px;
  }

  .logo h1 {
    font-size: 24px;
  }

  .welcome-text {
    font-size: 16px;
  }

  .form-header {
    margin-bottom: 25px;
  }
}

@media (max-width: 480px) {
  .register-page {
    padding: 10px;
  }

  .register-form-wrapper {
    padding: 25px 20px;
  }

  .form-header {
    margin-bottom: 20px;
  }

  .logo h1 {
    font-size: 20px;
  }

  .welcome-text {
    font-size: 14px;
  }

  .register-form .el-form-item {
    margin-bottom: 16px;
  }

  .decoration-content h2 {
    font-size: 20px;
  }

  .decoration-content p {
    font-size: 13px;
  }
}
</style>

<style scoped>
.register-page {
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.register-container {
  display: flex;
  background: white;
  border-radius: 12px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  max-width: 1000px;
  width: 100%;
  min-height: 600px;
}

/* 注册表单区域 */
.register-form-wrapper {
  flex: 1.2;
  padding: 40px 50px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  overflow-y: auto;
}

.form-header {
  text-align: center;
  margin-bottom: 30px;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 15px;
}

.logo .el-icon {
  color: #409eff;
}

.logo h1 {
  margin: 0;
  font-size: 28px;
  color: #303133;
  font-weight: bold;
}

.welcome-text {
  margin: 0;
  font-size: 18px;
  color: #606266;
}

.register-form {
  max-width: 400px;
  margin: 0 auto;
  width: 100%;
}

.register-form .el-form-item {
  margin-bottom: 20px;
}

.register-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  border-radius: 6px;
}

.form-footer {
  text-align: center;
  margin-top: 15px;
  color: #909399;
}

.form-footer .el-button {
  color: #409eff;
  padding: 0;
}

/* 右侧装饰区域 */
.register-decoration {
  flex: 0.8;
  background: linear-gradient(135deg, #36cfc9 0%, #409eff 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  position: relative;
  overflow: hidden;
}

.register-decoration::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><circle cx="50" cy="50" r="1.5" fill="rgba(255,255,255,0.1)"/></svg>') repeat;
  animation: float 25s infinite linear;
}

@keyframes float {
  0% {
    transform: translateX(0) translateY(0) rotate(0deg);
  }
  100% {
    transform: translateX(-50px) translateY(-50px) rotate(360deg);
  }
}

.decoration-content {
  text-align: center;
  z-index: 1;
  position: relative;
}

.decoration-content h2 {
  font-size: 32px;
  margin: 0 0 20px 0;
  font-weight: 300;
}

.decoration-content p {
  font-size: 16px;
  margin: 12px 0;
  opacity: 0.9;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .register-container {
    flex-direction: column;
    margin: 10px;
  }

  .register-decoration {
    order: -1;
    min-height: 180px;
  }

  .decoration-content h2 {
    font-size: 24px;
  }

  .decoration-content p {
    font-size: 14px;
    margin: 8px 0;
  }

  .register-form-wrapper {
    padding: 30px 30px;
  }

  .logo h1 {
    font-size: 24px;
  }

  .welcome-text {
    font-size: 16px;
  }

  .form-header {
    margin-bottom: 25px;
  }
}

@media (max-width: 480px) {
  .register-page {
    padding: 10px;
  }

  .register-form-wrapper {
    padding: 25px 20px;
  }

  .form-header {
    margin-bottom: 20px;
  }

  .logo h1 {
    font-size: 20px;
  }

  .welcome-text {
    font-size: 14px;
  }

  .register-form .el-form-item {
    margin-bottom: 16px;
  }

  .decoration-content h2 {
    font-size: 20px;
  }

  .decoration-content p {
    font-size: 13px;
  }
}
</style>
